package com.ruoyi.StarStudio.Service.Impl;

import com.ruoyi.StarStudio.Domain.Soil;
import com.ruoyi.StarStudio.Mapper.SoilMapper;
import com.ruoyi.StarStudio.Mapper.ZoneMapper;
import com.ruoyi.StarStudio.Service.SoilService;
import com.ruoyi.common.core.redis.RedisCache;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class SoilServiceImpl implements SoilService {

    @Autowired
    private SoilMapper soilMapper;

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ZoneMapper zoneMapper;

    //private static final Logger log = LoggerFactory.getLogger(SoilServiceImpl.class);

    //获取全部土壤信息
    @Override
    public List<Soil> getAll() {
        return soilMapper.getAll();
    }

    /**
     * 根据土壤id获取该条数据
     * @param soil_id 土壤数据ID
     * @return 土壤对象
     */
    @Override
    public Soil getBySoilId(long soil_id) {
        return soilMapper.getBySoilId(soil_id);
    }

    //根据地块id查询土壤信息
    @Override
    public List<Soil> getAllByZoneId(Integer zone_id) {
        return soilMapper.getAllByZoneId(zone_id);
    }

    //根据地块id查询最新一条土壤信息
    @Override
    public Soil getNewOneByZoneId(Integer zone_id) {
        int maxSoilIdFromZone_soil = soilMapper.findMaxSoilIdFromZone_Soil(zone_id);
        return soilMapper.getBySoilId(maxSoilIdFromZone_soil);
    }

    //根据地块id增加土壤信息及中间表数据
    @Override
    public void add(Soil soil, Integer zone_id) {
        soilMapper.add(soil);  //增加至主表
        soilMapper.addToZone_Soil(zone_id, soil.getSoil_id());  //增加至中间表
    }

    /**
     * 从 Redis中获取土壤相关的传感器数据，保存到数据库中
     */
    public void SensorAdd() {
        new Thread(()->{
            List<Integer> zoneIdList = zoneMapper.getZoneIdList();
            while (true) {
                for (Integer zoneID : zoneIdList) {
                    Double soil_temperature = redisCache.getCacheObject("soil_temperature_"+zoneID);
                    Double soil_humidity = redisCache.getCacheObject("soil_humidity_"+zoneID);
                    Double soil_ph = redisCache.getCacheObject("soil_ph_"+zoneID);
                    if (soil_temperature != null && soil_humidity != null && soil_ph != null){
                        Soil soil = new Soil();
                        soil.setSoil_temperature(soil_temperature);
                        soil.setSoil_moisture(soil_humidity);
                        soil.setSoil_ph(soil_ph);
                        soil.setSoil_updateTime(new Timestamp(System.currentTimeMillis()));
                        soilMapper.add(soil);
                        soilMapper.addToZone_Soil(zoneID, soil.getSoil_id());
                    }
                }
                try {
                    //每隔 5s 从 Redis中获取数据并存储
                    TimeUnit.SECONDS.sleep(5);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }).start();
    }

    /**
     * 定时任务   每天00:30:00清除最早一天的土壤数据 （ 17280条）
     */
    @Scheduled(cron = "0 30 0 * * ?")
    public void AutoDeleteSoilData(){
        List<Long> autoDeleteSoilIds = soilMapper.findAutoDeleteSoilIds();
        if (autoDeleteSoilIds.size() > 0){
            for (Long autoDeleteSoilId : soilMapper.findAutoDeleteSoilIds()) {
                soilMapper.delete(autoDeleteSoilId);
                soilMapper.deleteFromZone_Soil(autoDeleteSoilId);
            }
        }
    }

    //修改土壤信息
    @Override
    public void update(Soil soil) {
        soilMapper.update(soil);
    }

    //根据土壤id删除土壤信息
    @Override
    public void delete(long soil_id) {
        soilMapper.deleteFromZone_Soil(soil_id);
        soilMapper.delete(soil_id);
    }

    //带搜索查询
    @Override
    public List<Soil> getList(Soil soil) {
        return soilMapper.getList(soil);
    }
}
